﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using JoceLinItemDemo.Application.UserInfoMessage;
using JoceLinItemDemo.Application.UserInfoMessage.Dto;
using JoceLinItemDemo.Domain.UserInfoMessage;
using System.Threading.Tasks;
using AutoMapper;
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace JoceLinItemDemo.API.Controllers
{
    [Area("UserInfoMessage")]
    [Route("api/[area]/[controller]/[action]")]
    [ApiController]
    public class UserInfoMessageController : ControllerBase
    {
        //注入服务
        private readonly IUserInfoMessageAppService _userinfomessageservice;

        private readonly IWebHostEnvironment _webHostEnvironment;
        public UserInfoMessageController(IUserInfoMessageAppService userinfomessageservice, IWebHostEnvironment webHostEnvironment)
        {
            _userinfomessageservice = userinfomessageservice;
            _webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userInfoMessageDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<UserInfoMe> InsertUserInfoManager(UserInfoMessageDto userInfoMessageDto)
        {
            return await _userinfomessageservice.InsertUserInfoManager(userInfoMessageDto);
        }
        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns> 
        [HttpDelete]
        public async Task DeleteUserInfoManager(string Id)
        {
            await _userinfomessageservice.DeleteUserInfoManager(Id);
        }
        /// <summary>
        /// 批量删除用户信息
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task ManyDeleteCarManagerInfo(string Ids)
        {
            await _userinfomessageservice.DeleteUserInfoManagerByAny(Ids);
        }
        /// <summary>
        /// 查看用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult FindUserInfoManager(string Id)
        {
            return Ok(_userinfomessageservice.FindUserInfoManager(Id));
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfoMessageDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> UpdateUserInfoManager(UserInfoMessageDto userInfoMessageDto)
        {
            return await _userinfomessageservice.UpdateUserInfoManager(userInfoMessageDto);
        }
        /// <summary>
        /// 分页显示用户信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserInfoManagerByPage(int pageIndex, int pageSize) 
        {
            var list = _userinfomessageservice.GetUserInfoManagerByPage(pageIndex, pageSize);

            return Ok(list);
        }

        /// <summary>
        /// Excel文件模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DownloadExcelCase() 
        {
            //string sc = Path.AltDirectorySeparatorChar.ToString();
            //string filePath = $"{_webHostEnvironment.WebRootPath.Replace("\\", sc) + sc}Content{sc}ExcelFile{sc}123.xlsx";

            //项目发布后file=excel模板所在路径即可
            string file = "E:\\ABP前后端分离\\JoceLinItemDemo\\JoceLinItemDemo.API\\Content\\ExcelFile\\用户信息.xlsx";
            var stream = System.IO.File.OpenRead(file);
            return File(stream, "application/vnd.android.package-archive", Path.GetFileName(file));
        }
    }
}
